<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 卡片头部 -->
            <div slot="header">
                <el-row :gutter="20">
                    <el-col :span="7">
                        <el-input placeholder="请输入订单编号" clearable v-model="queryInfo.query" @clear="getOrderList">
                            <el-button slot="append" icon="el-icon-search" @click="getOrderList"
                                @keyup.enter="keyDown(e)"></el-button>
                        </el-input>
                    </el-col>
                </el-row>
            </div>

            <!-- 卡片主体 -->
            <div>
                <!-- 表格区域 -->
                <el-table :data="orderList" border stripe>
                    <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
                    <el-table-column prop="order_number" label="订单编号"></el-table-column>
                    <el-table-column prop="order_price" label="订单价格" align="center" width="100"></el-table-column>
                    <el-table-column prop="pay_status" label="是否付款" align="center" width="80">
                        <template slot-scope="scope">
                            <el-tag type="success" v-if="scope.row.pay_status === 1">已付款</el-tag>
                            <el-tag type="danger" v-else>未付款</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="is_send" label="是否发货" align="center" width="80">
                        <template slot-scope="scope">
                            <el-tag type="success" v-if="scope.row.is_send === 1">已发货</el-tag>
                            <el-tag type="danger" v-else>未发货</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="create_time" label="下单时间" align="center" width="180">
                        <template slot-scope="scope">
                            {{scope.row.create_time | dateFormat}}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="250">
                        <template>
                            <el-button size="mini" type="primary" icon="el-icon-edit"
                                @click="addressDialogVisible = true">
                                修改地址
                            </el-button>
                            <el-button size="mini" type="success" icon="el-icon-map-location" @click="logisticsInfo">
                                物流信息
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页 -->
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper" :total="orderTotal" background>
                </el-pagination>
            </div>
        </el-card>

        <!-- 修改地址的对话框 -->
        <el-dialog title="修改地址" :visible.sync="addressDialogVisible" width="30%" :close-on-click-modal="false"
            @close="addressDialogClose">
            <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px"
                status-icon>
                <el-form-item label="省市区/县" prop="address1">
                    <el-cascader v-model="addressForm.address1" :options="cityData" :props="addressProps" clearable
                        filterable style="width: 100%;"></el-cascader>
                </el-form-item>
                <el-form-item label="详细地址" prop="address2">
                    <el-input v-model="addressForm.address2">
                    </el-input>
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button @click="addressDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addressSubmit()">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 物流信息的对话框 -->
        <el-dialog title="物流信息" :visible.sync="logisticsInfoDialogVisible" width="30%" :close-on-click-modal="false">
            <el-timeline :reverse="reverse">
                <el-timeline-item v-for="(activity, index) in activities" :key="index" :timestamp="activity.timestamp"
                    :color="activity.color">
                    {{activity.content}}
                </el-timeline-item>
            </el-timeline>
            <span slot="footer">
                <el-button type="primary" @click="logisticsInfoDialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
// 导入城市数据
import cityData from "../assets/js/citydata";

export default {
    data() {
        return {
            // 订单列表
            orderList: [],
            // 获取订单列表数据所需参数
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 10
            },
            // 订单总数
            orderTotal: 0,
            // 修改地址对话框的显示与隐藏
            addressDialogVisible: false,
            // 修改地址表单的数据绑定对象
            addressForm: {
                address1: [],
                address2: ''
            },
            // 修改地址的表单验证对象
            addressFormRules: {
                address1: [{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
                address2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
            },
            // 城市数据
            cityData,
            // 修改地址级联选择器的配置对象
            addressProps: {
                expandTrigger: 'hover'
            },
            // 物流信息对话框的显示与隐藏
            logisticsInfoDialogVisible: false,
            // 时间线顺序
            reverse: false,
            // 时间线数据
            activities: [{
                content: '已签收,感谢使用顺丰,期待再次为您服务',
                timestamp: '2022-09-20',
                color: '#0bbd87'
            }, {
                content: '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
                timestamp: '2022-09-20'
            }, {
                content: '快件到达 [北京海淀育新小区营业点]',
                timestamp: '2022-09-19'
            }, {
                content: '快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]',
                timestamp: '2022-09-19'
            }, {
                content: '快件到达 [北京顺义集散中心]",',
                timestamp: '2022-09-19'
            }, {
                content: '顺丰速运 已收取快件',
                timestamp: '2022-09-19'
            }, {
                content: '卖家发货',
                timestamp: '2022-09-18'
            }, {
                content: '商品已经下单',
                timestamp: '2022-09-18'
            },]
        }
    },
    created() {
        // 获取订单列表
        this.getOrderList();
    },
    methods: {
        // 获取订单列表
        async getOrderList() {
            const { data: res } = await this.$http.get('orders', {
                params: this.queryInfo
            });
            if (res.meta.status !== 200) return this.$message.error('订单列表获取失败');
            this.orderList = res.data.goods;
            this.orderTotal = res.data.total;
        },
        // 处理每页显示条数的变化
        handleSizeChange(val) {
            this.queryInfo.pagesize = val;
            this.getOrderList();
        },
        // 处理当前页码的变化
        handleCurrentChange(val) {
            this.queryInfo.pagenum = val;
            this.getOrderList();
        },
        // 回车键搜索
        keyDown(e) {
            //如果是回车则执行搜索方法
            if (e.keyCode == 13) {
                this.getOrderList();
            }
        },
        // 监听修改地址对话框的关闭事件
        addressDialogClose() {
            this.$refs.addressFormRef.resetFields();
        },
        // 地址提交
        addressSubmit() {
            this.$notify.info({
                title: '消息',
                message: '功能都一样，不继续重复操作了'
            });
        },
        // 物流信息
        async logisticsInfo() {
            // 获取物流信息数据
            // const { data: res } = await this.$http.get('/kuaidi/:1106975712662')
            // 打开对话框
            this.logisticsInfoDialogVisible = true
        }
    },
    // 回车键搜索 绑定监听事件
    mounted() {
        window.addEventListener('keydown', this.keyDown);
    },
    // 回车键搜索 摧毁事件
    destroyed() {
        window.removeEventListener('keydown', this.keyDown, false);
    }
}
</script>

<style lang="less" scoped>

</style>
